package com.ruoyi.business.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.business.service.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusHardwareStatusUpdateMapper;
import com.ruoyi.business.domain.BusHardwareStatusUpdate;
import org.springframework.transaction.annotation.Transactional;

/**
 * 硬件设备状态更新记录Service业务层处理
 * 
 * @author asher
 * @date 2021-10-24
 */
@Service
public class BusHardwareStatusUpdateServiceImpl implements IBusHardwareStatusUpdateService 
{
    @Autowired
    private BusHardwareStatusUpdateMapper busHardwareStatusUpdateMapper;

    @Autowired
    @Qualifier("busSensorServiceImpl")
    private BusSensorServiceImpl busSensorService;

    @Autowired
    @Qualifier("busAcquisitionCardServiceImpl")
    private BusAcquisitionCardServiceImpl busAcquisitionCardService;

    @Autowired
    @Qualifier("busDeviceServiceImpl")
    private BusDeviceServiceImpl busDeviceService;

    /**
     * 查询硬件设备状态更新记录
     * 
     * @param recordId 硬件设备状态更新记录主键
     * @return 硬件设备状态更新记录
     */
    @Override
    public BusHardwareStatusUpdate selectBusHardwareStatusUpdateByRecordId(Long recordId)
    {
        return busHardwareStatusUpdateMapper.selectBusHardwareStatusUpdateByRecordId(recordId);
    }

    /**
     * 查询硬件设备状态更新记录列表
     * 
     * @param busHardwareStatusUpdate 硬件设备状态更新记录
     * @return 硬件设备状态更新记录
     */
    @Override
    public List<BusHardwareStatusUpdate> selectBusHardwareStatusUpdateList(BusHardwareStatusUpdate busHardwareStatusUpdate)
    {
        return busHardwareStatusUpdateMapper.selectBusHardwareStatusUpdateList(busHardwareStatusUpdate);
    }

    /**
     * 新增硬件设备状态更新记录
     * 
     * @param busHardwareStatusUpdate 硬件设备状态更新记录
     * @return 结果
     */
    @Override
    public int insertBusHardwareStatusUpdate(BusHardwareStatusUpdate busHardwareStatusUpdate)
    {
        busHardwareStatusUpdate.setCreateTime(DateUtils.getNowDate());
        return busHardwareStatusUpdateMapper.insertBusHardwareStatusUpdate(busHardwareStatusUpdate);
    }

    /**
     * 修改硬件设备状态更新记录
     * 
     * @param busHardwareStatusUpdate 硬件设备状态更新记录
     * @return 结果
     */
    @Override
    public int updateBusHardwareStatusUpdate(BusHardwareStatusUpdate busHardwareStatusUpdate)
    {
        busHardwareStatusUpdate.setUpdateTime(DateUtils.getNowDate());
        return busHardwareStatusUpdateMapper.updateBusHardwareStatusUpdate(busHardwareStatusUpdate);
    }

    /**
     * 批量删除硬件设备状态更新记录
     * 
     * @param recordIds 需要删除的硬件设备状态更新记录主键
     * @return 结果
     */
    @Override
    public int deleteBusHardwareStatusUpdateByRecordIds(Long[] recordIds)
    {
        return busHardwareStatusUpdateMapper.deleteBusHardwareStatusUpdateByRecordIds(recordIds);
    }

    /**
     * 删除硬件设备状态更新记录信息
     * 
     * @param recordId 硬件设备状态更新记录主键
     * @return 结果
     */
    @Override
    public int deleteBusHardwareStatusUpdateByRecordId(Long recordId)
    {
        return busHardwareStatusUpdateMapper.deleteBusHardwareStatusUpdateByRecordId(recordId);
    }

    @Transactional
    @Override
    public int updateStatusByDestRecordId(BusHardwareStatusUpdate busHardwareStatusUpdate) {
        HashMap<String, IBusHardwareStatusCommonUpdateService> updateTypeMap = new HashMap<String, IBusHardwareStatusCommonUpdateService>(){
            {
                put("SENSOR", busSensorService);
                put("ACQUISITION_CARD", busAcquisitionCardService);
                put("DEVICE", busDeviceService);
            }
        };

        HashMap<String, String> destTableMap = new HashMap<String, String>(){
            {
                put("SENSOR", "bus_sensor");
                put("ACQUISITION_CARD", "bus_acquisition_card");
                put("DEVICE", "bus_device");
            }
        };
        String updateType = busHardwareStatusUpdate.getUpdateType();
        IBusHardwareStatusCommonUpdateService iBusHardwareStatusCommonUpdateService = updateTypeMap.get(updateType);
        int updateResult = iBusHardwareStatusCommonUpdateService.updateStatusByDestRecordId(busHardwareStatusUpdate.getDestRecordId(), busHardwareStatusUpdate.getDestStatus(), busHardwareStatusUpdate.getUpdateStatusTime());
        busHardwareStatusUpdate.setDestTable(destTableMap.get(updateType));
        int insertResult = this.insertBusHardwareStatusUpdate(busHardwareStatusUpdate);
        return updateResult & insertResult;
    }
}
